package com.iims.flowable.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.iims.common.annotation.Log;
import com.iims.common.constant.UserConstants;
import com.iims.common.core.controller.BaseController;
import com.iims.common.core.domain.AjaxResult;
import com.iims.common.core.page.TableDataInfo;
import com.iims.common.enums.BusinessType;
import com.iims.flowable.flow.domain.FlowableForm;
import com.iims.flowable.flow.vo.request.DeployFormRequest;
import com.iims.flowable.service.IFlowFormService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * 流程表单Controller
 *
 * @author fanpei
 * @date 2021-04-03
 */
@Api(tags = "流程表单")
@ApiSort(5)
@Slf4j
@RestController
@RequestMapping("/flowable/form")
public class FlowFormController extends BaseController {
    @Autowired
    private IFlowFormService cmisFormService;

    //@Autowired
    // private FormRepositoryService formRepositoryService;

    //@Autowired
    //private RepositoryService repositoryService;


    /**
     * 查询流程表单列表
     */
    @ApiOperation("查询流程表单列表")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前记录起始索引(从1开始)", name = "pageNum", paramType = "query", required = true),
            @ApiImplicitParam(value = "每页显示记录数（>0）", name = "pageSize", paramType = "query", required = true),
            @ApiImplicitParam(value = "排序列", name = "orderByColumn", paramType = "query"),
            @ApiImplicitParam(value = "升降序方式：0升序 1降序", name = "isAsc", paramType = "query"),
            @ApiImplicitParam(value = "表单名称", name = "formName", paramType = "query"),
            @ApiImplicitParam(value = "表单Key", name = "formKey", paramType = "query")}
    )
    @PreAuthorize("@ss.hasPermi('flowable:form:list')")
    @GetMapping("/list")
    public TableDataInfo list(@ApiIgnore FlowableForm form) throws Exception {
        setSearchTimeRange(form);
        startPage();
        List<FlowableForm> list = cmisFormService.selectFlowableFormList(form);
        return getDataTable(list);
    }

    /**
     * 获取流程表单详细信息
     */
    @ApiOperation("获取流程表单详细信息")
    @ApiOperationSupport(order = 2)
    @PreAuthorize("@ss.hasPermi('flowable:form:query')")
    @GetMapping(value = "/{formKey}")
    public AjaxResult getInfo(@ApiParam(value = "表单Key", required = true) @PathVariable("formKey") String formKey) {
        return AjaxResult.success(cmisFormService.selectFlowableFormByKey(formKey));
    }

    /**
     * 新增流程表单
     */
    @ApiOperation("新增流程表单")
    @ApiOperationSupport(order = 3)
    @PreAuthorize("@ss.hasPermi('flowable:form:add')")
    @Log(title = "流程表单", businessType = BusinessType.INSERT)
    @Transactional(rollbackFor = Exception.class)
    @PostMapping
    public AjaxResult add(@RequestBody FlowableForm form) {
        if (UserConstants.NOT_UNIQUE.equals(cmisFormService.checkFormNameUnique(form))) {
            return AjaxResult.error("新增表单'" + form.getFormName() + "'失败，表单名称已存在");
        }
        return toAjax(cmisFormService.insertFlowableForm(form));
    }

    /**
     * 修改流程表单
     */
    @ApiOperation("修改流程表单")
    @ApiOperationSupport(order = 4)
    @PreAuthorize("@ss.hasPermi('flowable:form:edit')")
    @Log(title = "流程表单", businessType = BusinessType.UPDATE)
    @Transactional(rollbackFor = Exception.class)
    @PutMapping
    public AjaxResult edit(@RequestBody FlowableForm form) {
        if (UserConstants.NOT_UNIQUE.equals(cmisFormService.checkFormNameUnique(form))) {
            return AjaxResult.error("修改表单'" + form.getFormName() + "'失败，表单名称已存在");
        }
        return toAjax(cmisFormService.updateFlowableForm(form));
    }

    /**
     * 删除流程表单
     */
    @ApiOperation("删除流程表单")
    @ApiOperationSupport(order = 5)
    @PreAuthorize("@ss.hasPermi('flowable:form:remove')")
    @Log(title = "流程表单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{formKeys}")
    public AjaxResult remove(@PathVariable Long formKey) {
        return toAjax(cmisFormService.deleteFlowableFormById(formKey));
    }

    @ApiOperation(value = "部署表单")
    @ApiOperationSupport(order = 6)
    @Log(title = "部署表单")
    @PreAuthorize("@ss.hasPermi('flowable:form:deploy')")
    @PostMapping(value = "/deploy")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult deployModel(@Validated @RequestBody DeployFormRequest deployRequest) {
        cmisFormService.deployForm(deployRequest);
        return AjaxResult.success();
    }

}
